<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_plant_grass\Entity\ShusyPlantGrass;
use EntityListWrapper;

/**
 * @OA\Schema(
 *   schema="PlantGrassInfo",
 *   type="object",
 *   description="PlantGrassInfo",
 *   allOf={
 *       @OA\Schema(ref="#/components/schemas/PlantGrassItem"),
 *   }
 * )
 */
class PlantGrassInfo extends PlantGrassItem {

  protected function setValue() {
    parent::setValue();
    if ($this->entity instanceof ShusyPlantGrass) {
      $images = [];
      $field_images = $this->wrapper->field_images;
      if ($field_images instanceof EntityListWrapper) {
        foreach ($field_images as $image) {
          $images[] = $this->imageStyleUrl('PlantGrassInfoImages', $image->value());
        }
      }
      $value = [
        /**
         * @OA\Property(
         *   property="images",
         *   type="array",
         *   description="轮播图",
         *   @OA\Items(type="string")
         * ),
         */
        'images' => $images,
        /**
         * @OA\Property(
         *   property="content",
         *   type="string",
         *   description="种草内容",
         * ),
         */
        'content' => (string) $this->entity->content,
        /**
         * @OA\Property(
         *   property="topics",
         *   type="array",
         *   description="种草话题",
         *   @OA\Items(ref="#/components/schemas/TopicItem")
         * ),
         */
        'topics' => $this->getTopics(),
        /**
         * @OA\Property(
         *   property="products",
         *   type="array",
         *   description="推荐产品",
         *   @OA\Items(ref="#/components/schemas/ProductItem")
         * ),
         */
        'products' => $this->getProducts(),
        'created' => $this->formatDate($this->entity->created, 'Y/m/d'),
      ];
      $this->merge($value);
    }
  }

  protected function getTopics() {
    $topics = [];
    $field_topics = $this->wrapper->field_topics;
    if ($field_topics instanceof EntityListWrapper) {
      foreach ($field_topics as $field_topic) {
        $topics[] = TopicItem::get($field_topic->id->value());
      }
    }
    return $topics;
  }

  protected function getProducts() {
    $list = [];
    $field_products = $this->wrapper->field_products;
    if ($field_products instanceof EntityListWrapper) {
      foreach ($field_products as $field_product) {
        $list[] = ProductItem::get($field_product->id->value());
      }
    }
    return $list;
  }
}
